package com.example.flowerpcback.services;

import com.example.flowerpcback.dto.Roles;
import com.example.flowerpcback.dto.RolesExample;
import com.example.flowerpcback.dto.Tag;
import com.example.flowerpcback.dto.TagExample;
import com.example.flowerpcback.mapper.RolesMapper;
import com.example.flowerpcback.mapper.TagMapper;
import com.example.flowerpcback.req.RolesReq;
import com.example.flowerpcback.req.TagReq;
import com.example.flowerpcback.resp.PageResp;
import com.example.flowerpcback.resp.RolesResp;
import com.example.flowerpcback.resp.TagResp;
import com.example.flowerpcback.utils.CopyUtil;
import com.example.flowerpcback.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TagService {
    private static final Logger LOG = LoggerFactory.getLogger(TagService.class);

    @Resource
    private TagMapper tagMapper;

    @Resource
    private SnowFlake snowFlake;

    public TagResp save(TagReq req){

        Tag tag = new CopyUtil().copy(req, Tag.class);
        if(req.getId() == null){
            Long id = snowFlake.nextId();
            req.setId(id);
            tagMapper.insert(tag);
        }else {
            tagMapper.updateByPrimaryKey(tag);
        }

        TagResp res = new CopyUtil().copy(tag, TagResp.class);
        return res;

    }

    public PageResp<TagResp> list(TagReq req) {
        TagExample tagExample = new TagExample();
        TagExample.Criteria criteria = tagExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<Tag> tagList = tagMapper.selectByExample(tagExample);

        PageInfo<Tag> pageInfo = new PageInfo<>(tagList);
        PageResp<TagResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());

        List<TagResp> tagRespList = new CopyUtil().copyList(tagList,TagResp.class);

        pageResp.setList(tagRespList);
        return pageResp;
    }

    public List<TagResp> all(TagReq req) {
        TagExample tagExample = new TagExample();
        TagExample.Criteria criteria = tagExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        List<Tag> tagList = tagMapper.selectByExample(tagExample);

        List<TagResp> tagRespList = new CopyUtil().copyList(tagList,TagResp.class);

        return tagRespList;
    }

    public void delete(TagReq req){
        tagMapper.deleteByPrimaryKey(req.getId());
    }

}
